﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace com.sharepointutils.application.Forms
{
    public partial class ListItemModifyForm : Form
    {
        Boolean DropDownFilled = false;
        SharePointConnection Connection;
        Dictionary<String, object> properties = new Dictionary<string, object>();
        List<ListViewItem> currentCollection;
        String currentContentTypes;
        String javascriptFunction;


        public ListItemModifyForm(SharePointConnection connection, Form parent)
        {
            InitializeComponent();

            this.MdiParent = parent;
            this.Text = "Modify ListItems - " + connection.List;
            this.Connection = connection;
            lbl_upload_current.Text = connection.Label;
            this.Tag = connection.Label;

        }

        private void ListItemModifyForm_Load(object sender, EventArgs e)
        {
            lsbox_fields.DataSource = null;
            if (Connection != null && !String.IsNullOrEmpty(Connection.List))
            {
                lsbox_fields.DataSource = SharePointUtilities.getListColumnsbyDictionary(Connection.URL, Connection.Credentials, Connection.List);
            }
        }

        private int UpdateCount = 0; 

        private void backgroundWorker_modify_DoWork(object sender, DoWorkEventArgs e)
        {
            if (String.IsNullOrEmpty(javascriptFunction))
            {
                UpdateCount = SharePointUtilities.updateListItems(Connection.URL, Connection.Credentials, Connection.List, textBox_query.Text, properties);
            }
            else
            {
                UpdateCount = SharePointUtilities.updateListItems(Connection.URL, Connection.Credentials, Connection.List, textBox_query.Text, properties, javascriptFunction);
            }        
        }

        private void backgroundWorker_modify_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {

        }

        private void backgroundWorker_modify_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            progressBar_main.Value = 100;
            label_progress.Text = "Updated " + UpdateCount + " Items";
        }

        private void btn_addProp_Click(object sender, EventArgs e)
        {
            try
            {
                if (!String.IsNullOrEmpty(combo_prop.Text) && lsbox_fields.SelectedIndex >= 0)
                {
                    var item = (KeyValuePair<String, String>)lsbox_fields.SelectedItem;
                    KeyValuePair<String, String> temp = new KeyValuePair<string, string>(item.Value, item.Key + " - " + combo_prop.Text);
                    properties.Add(item.Value, combo_prop.Text);
                    lstbox_props.Items.Add(temp);
                    combo_prop.Text = String.Empty;
                }
            }
            finally
            { }
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            foreach (int i in lstbox_props.CheckedIndices)
            {
                KeyValuePair<string, string> item = (KeyValuePair<String, String>)lstbox_props.Items[i];
                properties.Remove(item.Key);
                lstbox_props.Items.RemoveAt(i);
            }
        }

        private void button_run_Click(object sender, EventArgs e)
        {
            javascriptFunction = txt_code.Text;
            backgroundWorker_modify.RunWorkerAsync();
            progressBar_main.Value = 1;
            UpdateCount = 0;
        }
    }
}
